package leecode

import "strings"

func canConstruct(ransomNote string, magazine string) bool {
	//首先找magazine中各个字符出现的次数
	magazineMap := make(map[string]int, len(magazine))
	magazineArr := strings.Split(magazine, "")
	for _, value := range magazineArr {
		magazineMap[value] = magazineMap[value] + 1
	}
	//用map集合存储ransomNote中各个字符出现的次数
	ransomNoteMap := make(map[string]int, len(ransomNote))
	ransomNoteArr := strings.Split(ransomNote, "")
	for _, value := range ransomNoteArr {
		ransomNoteMap[value] = ransomNoteMap[value] + 1
	}
	//判断在 ransomNoteMap 中出现的字符  magazineMap 是否存在,并且次数是否相等
	for key, _ := range ransomNoteMap {
		if magazineMap[key] < ransomNoteMap[key] {
			return false
		}
	}
	return true
}
